home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / pack / ppdecrunch10.lzh / PPDecrunch.c < prev    next >
C/C++ Source or Header  |  1991-07-22  |  5KB  |  166 lines

  1. /*
  2. PPDecrunch Version 1.0
  3.  
  4. Pipeable PowerPacker Decruncher
  5.  
  6. Created on   June 30, 1991
  7. Last revised July 22, 1991
  8.  
  9. Copyright (C) 1991 by Darren Ewaniuk
  10. Freely distributable with conditions
  11. (See "distribution" section of the documentation file for more information)
  12.  
  13. Builds on example public domain code from PPLib file name "example.c"
  14. by Nico François.
  15. (Thanks Nico for the excellent PowerPacker library!)
  16.  
  17. Compiled using the Freely distributable DICE C compiler version 2.06.16
  18. (Thanks to Matt Dillon for DICE - as soon as it includes the 2.0
  19.  specific files, I'm sending in the registration for it)
  20.  
  21. Compile using "DCC PPDecrunch.c /PPLib/PPSCGlue.o -o PPDecrunch"
  22.  
  23. Library functions used:
  24.  
  25.     Exec
  26.         OpenLibrary     To open PowerPacker library
  27.         CloseLibrary    To close PowerPacker library
  28.         FreeMem         To free allocated memory
  29.  
  30.     PowerPacker.library V33
  31.         ppLoadData      Load PowerPacked file
  32. */
  33.  
  34. /* Pretend DICE is Aztec C so that ppbase.h does not try to use #pragmas */
  35.  
  36. #define AZTEC_C 1
  37.  
  38. /* Include the necessary files */
  39.  
  40. #include <exec/types.h>
  41. #include <exec/memory.h>
  42. #include <libraries/dos.h>
  43. #include <libraries/ppbase.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46.  
  47. /* Set up library base */
  48.  
  49. struct PPBase *PPBase = NULL;
  50.  
  51. /* Kill DICE's break handling */
  52.  
  53. void chkabort()
  54. {
  55. }
  56.  
  57. /* Checks for break and returns TRUE if pressed */
  58.  
  59. int taskbreak(FILE *stderrfile)
  60. {
  61.     /* Check and clear break signal */
  62.  
  63.     if ((SetSignal(0L, SIGBREAKF_CTRL_C)) & SIGBREAKF_CTRL_C)
  64.     {
  65.         fputs ("\n***PPDecrunch BREAK\n", stderrfile);
  66.         return (TRUE);
  67.     }
  68.     else
  69.     {
  70.         return (FALSE);
  71.     }
  72. }
  73.  
  74. /* Start of the main program */
  75.  
  76. void main (int argc, char *argv[])
  77. {
  78.     UBYTE *filestart = NULL;    /* Memory address of decrunched file */
  79.     ULONG filelen;              /* Length of decrunched file */
  80.     ULONG bytecount = 0;        /* Counter for outputting the file */
  81.     int err;                    /* PowerPacker error number */
  82.     int warncode = 0;           /* Exit code for return when finished */
  83.     FILE *stderrfile;           /* File handle for error messages */
  84.  
  85.     /* Gimme an error channel back to the calling window */
  86.     /* so errors don't get redirected or go to the pipe */
  87.  
  88.     /* Get out of here if you can't even get a handle */
  89.     /* to the calling window */
  90.  
  91.     if (!(stderrfile = fopen ("*", "w")))
  92.     {
  93.         exit (11);
  94.     }
  95.     /* DICE leaves before 0 args are passed because there's no WBMain() */
  96.     /* but I thought I'd leave it in for people with other compilers */
  97.  
  98.     if (argc == 0)          /* Too bad, cannot run from Workbench. */
  99.     {
  100.         exit (0);
  101.     }
  102.     else if (argc !=2)      /* User doesn't know what he's doing! */
  103.     {
  104.         fputs ("\n\033[33mPPDecrunch 1.0\033[31m (July 22, 1991) Pipeable PowerPacker Decruncher\n", stderrfile);
  105.         fputs ("Copyright \251 1991 by Darren Ewaniuk, freely distributable with conditions\n", stderrfile);
  106.         fputs ("Usage:  PPDecrunch <filename>\n", stderrfile);
  107.         fputs ("        Output is sent to STDOUT, useful for piping into text readers\n", stderrfile);
  108.         fputs ("        or redirecting to other files or devices\n", stderrfile);
  109.         fputs ("        Example: 'PPDecrunch <filename> | more'\n\n", stderrfile);
  110.         warncode = 1;
  111.     }
  112.     else                        /* Right number of arguments.  Lets go! */
  113.     {
  114.         /* Open PowerPacker.library */
  115.  
  116.         if (!(PPBase = (struct PPBase *)OpenLibrary ("powerpacker.library", 0L)))
  117.         {
  118.             fputs ("\n\033[33mPPDecrunch Error:  PowerPacker library V33+ required\033[31m\n\n", stderrfile);
  119.             warncode = 11;
  120.         }
  121.         else
  122.         {
  123.             if (err = ppLoadData (argv[1], DECR_NONE, 0L, &filestart, &filelen, -1))
  124.             {
  125.                 fputs ("\n\033[33mPPDecrunch Error:  ", stderrfile);
  126.                 switch (err)
  127.                 {
  128.                     case PP_READERR:
  129.                         fputs ("Error loading text file", stderrfile);
  130.                         break;
  131.                     case PP_NOMEMORY:
  132.                         fputs ("No memory to decrunch file", stderrfile);
  133.                         break;
  134.                     case PP_PASSERR:
  135.                         fputs ("Password protected, loading aborted", stderrfile);
  136.                         break;
  137.                     case PP_OPENERR:
  138.                         fputs ("Cannot open file", stderrfile);
  139.                         break;
  140.                     case PP_UNKNOWNPP:
  141.                         fputs ("Crunched with unknown PP", stderrfile);
  142.                         break;
  143.                     default:
  144.                         fputs ("Unknown error", stderrfile);
  145.                         break;
  146.                 }
  147.                 fputs ("!\033[31m\n\n", stderrfile);
  148.                 warncode = 10;
  149.             }
  150.             else
  151.             {
  152.                 while ((bytecount < filelen) && (!taskbreak(stderrfile)))
  153.                 {
  154.                     fputc (filestart[bytecount++], stdout);
  155.                 }
  156.                 /* Free all resources */
  157.  
  158.                 FreeMem (filestart, filelen);
  159.             }
  160.             CloseLibrary ((struct Library *)PPBase);
  161.         }
  162.     }
  163.     fclose (stderrfile);
  164.     exit (warncode);
  165. }
  166.